<?php

/* 
 * 模块
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storyModuleController extends AppController
{
    public $class = 'storymodule';
    public function __construct() {
        parent::__construct();
        $this->modelObj = new storyModuleModel();
        $this->log_type = '模块管理';
        $this->title = $this->log_type;
        $this->cookie_key = 'story_module_cookie';
        $this->assign('class', $this->class);
    }
    public function view(){
        $this->_get_data();
        $where = array('is_show' => 1);
        $data = storySourceModel::load()->getAll(null, $where);      
        foreach ($data as $key => $row) {
            $data[$key]['ssid'] = $row['sid'];
        }
        $this->assign('source', $data);
        $this->display('module_list');
    }
    public function query(){
        $this->_get_data();
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('module_list');
        exit(json_encode($this->ret));
    }

    public function create(){
        $this->create_token();
        $this->get_cookie();
        $where = array('is_show' => 1);
        $data = storySourceModel::load()->getAll(null, $where);      
        foreach ($data as $key => $row) {
            $data[$key]['ssid'] = $row['sid'];
        }
        $this->assign('data', $data);
        $this->display('module_create');
    }
    public function new_create(){
        $this->check_token();
        $data = array(
            'sid' => intval($_POST['sid']),
            'mname' => trim($_POST['mname']),
            'url' => trim($_POST['url']),
            'in_time' => p_getTime(),
        );
        $this->set_cookie($data);
        if($data['sid'] <= 0) {
            $this->show_msg('选择来源名称');
        }
        if(empty($data['mname'])) {
            $this->show_msg('名称不能为空');
        }
        if(empty($data['url'])) {
            $this->show_msg('URL不能为空');
        }
        $where = array(
            'mname' => $data['mname'],
        );
        if($this->modelObj->getRow(null, $where)) {
            $this->show_msg('名称重复');
        }
        $bool = $this->modelObj->insert($data);
        if($bool) {
            $this->admin_log('添加模块' . $data['mname']);
            $this->show_msg('添加成功', 'c=' . $this->class . '&m=view');
        } else {
            $this->show_msg('添加失败');
        }
    }
    public function del(){
        $id = $_POST['id'];
        if($id > 0) {
            if(storyTitleModel::load()->getMid($id)) {
                echo $this->setReturn(100, '不能删除，因为在模块里含有数据');exit;
            } else {
                $this->remove();
            }
        }
        echo $this->setReturn(101, '删除失败');
    }
    /**
     * 来源获取数据
     */
    private function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['mname'] = isset($_POST['mname']) ? $_POST['mname'] : '';
        $filter['sid'] = isset($_POST['sid']) ? $_POST['sid'] : 0;
        if(!empty($filter['mname'])) {
            $where .= ' and instr(mname, ' . "'{$filter['mname']}')";
        }
        if($filter['sid'] > 0) {
            $where .= ' and sid = ' . $filter['sid'];
        }
        $key = $this->modelObj->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('story_module', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {                
                $data['pageData'][$key]['id'] = $key + 1;
                $data['pageData'][$key]['sname'] = storySourceModel::load()->getName($row['sid']);
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);               
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
}

